const Service = require("egg").Service;
const { Op } = require("sequelize");
class CacheService extends Service {
  constructor(ctx) {
    super(ctx);
    this.session = ctx.session;
    this.redis = ctx.service.tool.redis;
    this.ResponseCode = ctx.response.ResponseCode;
    this.ServerResponse = ctx.response.ServerResponse;
    this.underlineToBigHump = ctx.underlineToBigHump;
    this.objUnderlineToHump = ctx.objUnderlineToHump;
    this.objHumpToUnderline = ctx.objHumpToUnderline;
    this.toInt = ctx.toInt;
  }

  /**
   * 获取名称
   * @param {*} params
   */
  async getNames() {
    let data = await this.redis.get(0, "config_name");
    return this.ServerResponse.createBySuccessData(data);
  }

  /**
   * 获取键名
   */
  async getKeys(keys) {
    let data = await this.redis.get(0, keys);
    let rData = [];
    for (var key in data) {
      rData.push(key);
    }
    return this.ServerResponse.createBySuccessData(rData);
  }

  /**
   * 获取键值
   * @param {*} keyType
   * @param {*} key
   */
  async getValue(keyType, key) {
    let data = await this.redis.get(0, keyType);
    data = data[key];
    return this.ServerResponse.createBySuccessData({
      cacheKey: key,
      cacheName: keyType,
      cacheValue: typeof data == "object" ? JSON.stringify(data) : data,
      remark: "",
    });
  }
}

module.exports = CacheService;
